import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../components/Login.vue";
import Home from "../components/Home.vue";
import Main from "../components/Main.vue";
// import Users from "../components/users/Users.vue";
// import Rights from "../components/power/Rights.vue";
// import Roles from "../components/power/Roles.vue";
// import Report from "../components/report/Report.vue";
// import Categories from "../components/commodity/categories.vue";
import UserManager from "../components/users/UserManager.vue";
import DepartmentManager from "../components/users/DepartmentMananger.vue"; 
import Registlevel from "../components/users/Registlevel.vue";
import ConstantType from "../components/users/ConstantType.vue";
import SettlecategoryManager from "../components/users/SettlecategoryManager.vue";
import Scheduling from "../components/users/Scheduling.vue";
import Rule from "../components/users/Rule.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/login"
  },
  {
    path: "/login",
    component: Login
  },
  {
    path: "/home",
    component: Home,
    redirect: "/main",
    children: [
      {
        path: "/main",
        component: Main
      },
      // {
      //   path: "/users",
      //   component: Users
      // },
      // {
      //   path:'/rights',
      //   component:Rights
      // },
      // {
      //   path:'/roles',
      //   component:Roles
      // },
      // {path :'/reports',
      // component:Report},
      // {path :'/categories',
      // component:Categories}
      {path:'/usermanager',component:UserManager},
      {path:'/department',component:DepartmentManager},
      {path:'/registlevel',component:Registlevel},
      {path:'/constantitem',component:ConstantType},
      {path:'/settlecategory',component:SettlecategoryManager},
      {path:'/scheduling',component:Scheduling},
      {path:'/rule',component:Rule}
    ]
  },
 

 

];

const router = new VueRouter({
  routes
});

//如果用户退出登录，也就意味着用户不用访问首页(/home)了。
//通过挂载路由的导航守卫来实现这个功能
// to:就是我们要访问的路径
// from:表示从哪个路径跳转过来的
// next:它就是一个函数，表示放行
router.beforeEach((to, from, next) => {
  if (to.path === "/login") return next();
  //我们要获取到sessionStorage中的token
  const tokenS = window.sessionStorage.getItem("token");
  //如果token不存在，我们就要强制跳转
  if (!tokenS) return next("/login");
  //否则放行
  next();
});

export default router;
